﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Xml;
using System.Xml.Serialization;

namespace RapGetNet
{
    public partial class FirstForm : Form
    {
        public FirstForm()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        protected override void OnShown(EventArgs e)
        {
            //this.textBox1.SelectionLength = 0;
            //this.textBox1.SelectionStart = 0;
            textBox1.Focus();
            base.OnShown(e);
        }

        private void button1_Click(object sender, EventArgs e)
        {
           /* if (!File.Exists(@"data.xml"))
            {*/
                //try
                //{


                string strguser;
                string strgpass;
                strguser = textBox1.Text;
                strgpass = textBox2.Text;

                toolStripStatusLabel1.Text = "Connecting...";
                this.Refresh();
                string getVars = "?sub=getaccountdetails_v1&type=prem&login=" + strguser + "&password=" + strgpass + "&withcookie=1";
                HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(string.Format("http://api.rapidshare.com/cgi-bin/rsapi.cgi{0}", getVars));
                WebReq.Method = "GET";
                HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();


                Stream Answer = WebResp.GetResponseStream();
                StreamReader _Answer = new StreamReader(Answer);

                string information = _Answer.ReadToEnd();
                string accids = information.Substring(0, 9);
                if (accids == "accountid")
                {
                    toolStripStatusLabel1.Text = "Done";
                    this.Refresh();

                    XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                    TextWriter tw = new StreamWriter("data.xml");

                    int cc = information.IndexOf("cookie=");
                    string strcookie = information.Substring(cc + 7, 96);
                    //MessageBox.Show(strcookie);
                    Settings set = new Settings();
                    set.accountCookie = strcookie;
                    set.accountLogin = strguser;
                    set.accountPass = strgpass;
                    set.accountType = "prem";
                    string folderName;

                    folderBrowserDialog1.Description = "Select download path";
                    //folderBrowserDialog1.ShowDialog();
                    DialogResult result = folderBrowserDialog1.ShowDialog();
                    if (result == DialogResult.OK)
                    {
                        folderName = folderBrowserDialog1.SelectedPath;
                        set.downPath = folderName;

                        serializer.Serialize(tw, set);
                        tw.Close();

                        RapGet rpgt = new RapGet();
                        rpgt.Show();
                        this.Close();
                    }
                    if (result == DialogResult.Cancel)
                    {
                        //File.Delete(@"data.xml");
                        tw.Close();
                        File.Delete("data.xml");
                        Application.Exit();
                        
                       
                    }

                    //serializer.Serialize(tw, set);
                    //tw.Close();


                    //XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                    //TextWriter tw = new StreamWriter("data.xml");
                    //serializer.Serialize(tw, set);
                    //tw.Close();

                }
                else
                {
                    toolStripStatusLabel1.Text = information;
                    this.Refresh();

                }
                //}
                //catch (Exception ex)
                //{
                // toolStripStatusLabel1.Text = ex.ToString();
                // this.Refresh();
                //}
           /* }
            else 
            {
                RapGet rpgt = new RapGet();
                rpgt.Show();
                this.Close();
            }*/

        }

        private void button2_Click(object sender, EventArgs e)
        {   
            Application.Exit();
        }

        private void FirstForm_Load(object sender, EventArgs e)
        {
            
            


        }
    }
}
